<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/7/6
 */
namespace backend\controllers;
use Yii;
use common\helps;
use yii\base\Exception;
use yii\web\AdminController;
use backend\models\UserMaster;
use backend\models\UserMappedRole;
use common\helps\tools;
use backend\models\AuthAccess;

class LoginController extends AdminController{
    public $layout = false;                                                     //不继承主模板
    //业务关键字
    public $service = 'Login';

    public function actionIndex(){
        $this->saveLog($this->service,'登录页','VIEW','');                     //日志
        return $this->render('index');
    }

    /**
     * add by allen.qiang 20170710
     * 登录验证
     */
    public function actionLoginPost(){

        $user = Yii::$app->request->post('userName');
        $pwd = Yii::$app->request->post('userPwd');
        $user_data = $this->CheckUser($user,$pwd,$post);
        if(!$user_data){
            $this->saveLog($this->service,'用户登录失败','LOGIN','用户名密码不正确');                     //日志
            $this->retJson('用户名密码不正确',901);
            return;
        }
        $user_rule_arr = [];
        if ($user_data['id']){
            $user_rule_arr = tools::getUserRule($user_data['id'],$user_data['is_admin']);
        }
        $user_rule_arr = array_unique(array_filter($user_rule_arr));
        $redis = new helps\redisHelper();
        $redis->setLoginInfo(array(
            'userName'=>$user_data['user_name'],
            'userPwd'=>$pwd,
            'is_admin'=>$user_data['is_admin'],
            'userId'=>$user_data['id'],
            'user_rule'=>$user_rule_arr
        ));

        //TODO 修改最后登录
        //信息
        $data['last_login_time'] = date('Y-m-d H:i:s',time());
        $data['last_login_ip'] = $_SERVER['REMOTE_ADDR'];
        UserMaster::updateAll($data, " user_name = '".$user."'");

        $this->saveLog($this->service,'用户登录成功','LOGIN','登录成功');                     //日志
        $this->retJson('登录成功',200);
    }

    private function CheckUser($user,$pwd,&$post){
        try{
            $post = Yii::$app->db->createCommand('SELECT * FROM j_user_master WHERE BINARY user_name = :user_name')
                ->bindValue(':user_name', $user)
                ->queryOne();
            if(!$post){
                return false;
            }
            if($post['user_pwd'] != helps\tools::md5pwd($pwd)){
                return false;
            }
            return $post;
        }catch (Exception $e){
            //mod by allen.qiang 20180112 异常处理，日志优化 start
            //error_log($ex->getMessage());
            error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
            //mod by allen.qiang 20180112 异常处理，日志优化 end
            return false;
        }
    }

    /**
     * 退出登录
     */
    public function actionLoginOut(){
        $redis = new helps\redisHelper();
        $this->saveLog($this->service,'用户退出登录','LOGOUT','用户退出登录');                     //日志
        $redis->logOut();
        return $this->render('index');
    }
}